import { defineStore } from "pinia";
import {getCurrentGlobalProperties,  store} from "@frontend/vue-common";
import {
    listProductCustomFieldListRowAPI,
    addProductCustomFieldListRowAPI,
    getProductCustomFieldListRowAPI,
    editProductCustomFieldListRowAPI,
    deleteProductCustomFieldListRowAPI,
} from "@/api/productCustomFieldListRow";


const filter = class {
    constructor() {
        this.columnId = {value: null}
		this.jsonValue = {value: null}
		
    }
}

const form = class {
    constructor() {
        this.id = null
        this.columnId = null
		this.jsonValue = null
		
    }
}

const newStore = defineStore({
    id: "productCustomFieldListRow",
    state: () => ({
        list: [],
        pageNum: 1,
        pageSize: 20,
        totalCount: 0,
        sortField: "id",
        sortOrder: -1,
        filter: new filter,
        form: new form,
        statusOptions: [{code: 1, name: "待确认"}, {code: 2, name: "已上线"}, {code: 3, name: "待删除"},{code:4, name: "已删除"}],
        selected: [],
        dataList: [],
    }),
    actions: {
        cleanAll() {
            this.list = []
            this.pageNum = 1
            this.pageSize = 20
            this.totalCount = 0
            this.sortField = "id"
            this.sortOrder = -1
            this.filter = new filter
            this.form = new form
        },
        cleanFilter() {
            this.filter = new filter
        },
        cleanForm() {
            this.form = new form
        },
        async listProductCustomFieldListRow() {
            return new Promise((resolve, reject) => {
                listProductCustomFieldListRowAPI(this.pageNum, this.pageSize, this.sortField, this.sortOrder, {
                    columnId: this.filter.columnId.value,
					jsonValue: this.filter.jsonValue.value,
					
                })
                    .then((res) => {
                        this.list = res.data
                        this.totalCount = res.total
                        resolve()
                    })
                    .catch(err => {
                        reject(err);
                    });
            });
        },
        async addProductCustomFieldListRow(data) {
            return new Promise((resolve, reject) => {
                addProductCustomFieldListRowAPI(data)
                    .then(() => {
                        this.listProductCustomFieldListRow()
                            .then(()=>{
                                getCurrentGlobalProperties().$toast.add({severity: 'success', summary: '操作成功', detail: '', life: 2000});
                                resolve()
                            })
                            .catch(err => {
                                reject(err);
                            });
                    })
                    .catch(err => {
                        reject(err);
                    });
            });
        },
        async editProductCustomFieldListRow() {
            return new Promise((resolve, reject) => {
                editProductCustomFieldListRowAPI(this.form)
                    .then(() => {
                        this.listProductCustomFieldListRow()
                            .then(()=>{
                                getCurrentGlobalProperties().$toast.add({severity: 'success', summary: '操作成功', detail: '', life: 2000});
                                resolve()
                            })
                            .catch(err => {
                                reject(err);
                            });
                    })
                    .catch(err => {
                        reject(err)
                    })
            })
        },
        async getProductCustomFieldListRow(id) {
            return new Promise((resolve, reject) => {
                getProductCustomFieldListRowAPI({
                    id: id,
                })
                    .then((res) => {
                        this.form = res.data
                        resolve()
                    })
                    .catch(err => {
                        reject(err);
                    });
            });
        },
        async deleteProductCustomFieldListRow(ids) {
            return new Promise((resolve, reject) => {
                deleteProductCustomFieldListRowAPI(ids)
                    .then(() => {
                        this.listProductCustomFieldListRow()
                            .then(()=>{
                                getCurrentGlobalProperties().$toast.add({severity: 'success', summary: '操作成功', detail: '', life: 2000});
                                resolve()
                            })
                            .catch(err => {
                                reject(err);
                            });
                    })
                    .catch(err => {
                        reject(err);
                    });
            });
        },
    }
})

export default newStore(store)
